
// global全局方法, 不会引用Vue
let GLOBAL = ()=>{
    return {
        // 深拷贝复制对象
        deepClone (source) {
            // 判断复制的目标是数组还是对象
            const ConstructorIsArray = source.constructor === Array? true : false;
            const ConstructorIsObject = source.constructor === Object? true : false;
            
            let target = ConstructorIsArray? [] : (ConstructorIsObject?{}:source);
            
            for (let keys in source) {
                // 遍历目标
                if (source.hasOwnProperty(keys)) {
                    if (source[keys] && typeof source[keys] === "object") {
                        // 如果值是对象，就递归一下
                        target[keys] = source.constructor === Array ? [] : (source.constructor === Object?{}:source); 
                        target[keys] = this.deepClone(source[keys]);
                    } else {
                        // 如果不是，就直接赋值
                        target[keys] = source[keys];
                    }
                }
            }
            return target
        },

        /*
        * 防抖
        * fn [function] 需要防抖的函数
        * delay [number] 毫秒，防抖期限值
        */
        debounce(fn, delay){
            let timer = null; //借助闭包
            return function() {
                if(timer){
                    clearTimeout(timer);
                }
                timer = setTimeout(fn, delay); // 简化写法
            }
        },

        /*
        * 节流
        * fn [function] 需要防抖的函数
        * delay [number] 毫秒，防抖期限值
        */
        throttle(fn, delay){
            let valid = true;
            return function() {
                if(!valid){
                    //休息时间 暂不接客
                    return false
                }
               // 工作时间，执行函数并且在间隔期内把状态位设为无效
                valid = false;
                setTimeout(() => {
                    fn();
                    valid = true;
                }, delay)
            };
        },

        // 类型判断
        toString: target=>Object.prototype.toString.call(target).slice(8,-1),

        // 获取sessionStorage
        getSeesion () {
            let result = JSON.parse(sessionStorage.getItem(arguments[0]));
            return result
        },

        // 对象key值转换为小写
        toLowerObject (obj) {
            let newObj = {};
            for (var key in obj){
                newObj[key.toLowerCase()] = obj[key];
            };
            return newObj
        },

        // 对象key值转换为大写
        toUpperObject (obj) {
            let newObj = {};
            for (var key in obj){
                newObj[key.toUpperCase()] = obj[key];
            };
            return newObj
        }
    }
};
export default GLOBAL();
